const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));
const common = require('../../../common');
const Administrator = require('../../../../models/administrator');
const File = require('../../../../models/file');
const uri = require('../../uri');
const { versionType } = require('../../../../../phoenix/models/project-versions');
const benchmark = require('./../../../../benchmark/integration-test/tree');
// const rules = require('./../../../../benchmark/static-analyze/gjb.json');
module.exports = describe('/POST project-versions/:versionId/init.json', function () {
  const administrator = new Administrator();
  let project = {};
  let projectVersion = {};
  let staticAnalyzeVersion = {};
  const newUserIds = [];
  // let newUser = {};
  function url(versionId) {
    if (!versionId) {
      versionId = projectVersion.versionId;
    }
    return `/project-versions/${versionId}/init.json`;
  }
  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        // newUser = tester;
        return tester.createRandomProject();
      })
      .then((res) => {
        project = res;
        return project.createRandomVersion();
      })
      .then((res) => {
        projectVersion = res;
        return projectVersion.updateVersionFiles(benchmark.files);
      })
      .then(() => {
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'post');
  common.should.be.tester(uri, url, 'post');

  // 初始化版本之前只有主版本
  it('it should be 2 of init project versions', function (done) {
    project.getVersionTotalNumber()
      .then((totalNumber) => {
        // createRandomProject会创建出一个默认的随机版本
        totalNumber.should.be.eq(2);
        done();
      }).catch(done);
  });
  // 初始化集成测试版本
  it('it should POST success of init  integrationTest version ', function (done) {
    projectVersion.initSubVersion(
      projectVersion.versionId,
      versionType.integrationTest
    )
      .then((res) => project.getVersion(res.versionId))
      .then((res) => {
        res.project = projectVersion.project;
        const file = new File(res, res.rootDirectory.fileId);
        return file.getTree();
      })
      .then((files) => {
        common.should.be.equal.subVersion(files, benchmark.result);
        done();
      })
      .catch(done);
  });
  //  初始化静态分析版本
  it('it should POST success of init staticAnalyze version ', function (done) {
    projectVersion.initSubVersion(
      projectVersion.versionId,
      versionType.staticAnalyze
    )
      .then((res) => project.getVersion(res.versionId))
      // .then((res) => {
      //   res.project = projectVersion.project;
      //   staticAnalyzeVersion = res;
      //   return newUser.createTemplate(rules.verified_rules);
      // })
      // .then((template) => projectVersion.linkAnalyzeTemplate(template._id.toString()))
      .then((res) => {
        res.project = projectVersion.project;
        staticAnalyzeVersion = res;
        const file = new File(staticAnalyzeVersion, staticAnalyzeVersion.rootDirectory.fileId);
        return file.getTree();
      })
      .then((files) => {
        files.analyzeTemplate = staticAnalyzeVersion.analyzeTemplate;
        common.should.be.equal.subVersion(files, benchmark.result, false);
        done();
      })
      .catch(done);
  });
});
